package com.cspm.ahjy.bc.office.word.util;

import java.io.File;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.cspm.ahjy.bc.office.word.domain.MSBody;

/**
 * XML文件解析成对象工具类
 * @author ServerZhang
 * @date 2017年10月17日
 */
public class Xml2WordObjUtil {

	/**
	 * 解析MSBody对象
	 * @param path 文件所在目录
	 * @param fileName 文件名带后缀
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月17日
	 */
	public static MSBody getMSBody(String path, String fileName) {
		SAXReader reader = new SAXReader();
		Document document = null;
		try {
			document = reader.read(new File(path+fileName));
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Element root = document.getRootElement();
		
		Element bodyElement = root.element("body");
		
		MSBody msBody = BodyUtil.getMsBody(bodyElement,path);
		
		/*List<Element> elements = root.elements("body");
		Iterator<Element> bodyIterator = elements.iterator();
		
		List<MSParagraph> paragraphs = new ArrayList<MSParagraph>();
		while (bodyIterator.hasNext()) {
			Element bodyElement = (Element) bodyIterator.next();

			// TODO 获取body的属性

			Iterator sectIterator = bodyElement.elementIterator("sect");
			while (sectIterator.hasNext()) {
				Element next = (Element) sectIterator.next();
				
				//获取所有段
				
				paragraphs.addAll(getAllParagraph(next));
//System.out.println(paragraphs.size());
				
			}
			msBody.addParagraph(paragraphs);

		}*/
		return msBody;
	}
	
	
}
